﻿using Azrng.DbOperator;
using Azrng.DbOperator.Helper;
using DBCHM.Models;
using System;
using System.Threading.Tasks;

namespace DBCHM.Service
{
    public class BusinessService : IBusinessService
    {
        public async Task<bool> ConnectTestAsync(DbConnectConfigDto config)
        {
            // 连接测试
            IDbHelper dbHelper;
            var dbSourceConfig = new DataSourceConfig
                                 {
                                     Host = config.Host,
                                     Port = config.Port,
                                     DbName = config.DataBase,
                                     User = config.UserName,
                                     PassWord = config.Password,
                                 };
            switch (config.DbType)
            {
                case DatabaseType.MySql:
                    dbHelper = new MySqlDbHelper(dbSourceConfig);
                    break;
                case DatabaseType.SqlServer:
                    dbHelper = new SqlServerDbHelper(dbSourceConfig);
                    break;
                case DatabaseType.Oracle:
                    dbHelper = new OracleDbHelper(dbSourceConfig);
                    break;
                case DatabaseType.PostgresSql:
                    dbHelper = new PostgreSqlDbHelper(dbSourceConfig);
                    break;
                case DatabaseType.SqLite:
                    dbHelper = new SqliteDbHelper(dbSourceConfig);
                    break;
                case DatabaseType.ClickHouse:
                    dbHelper = new ClickHouseDbHelper(dbSourceConfig);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            var flag = await dbHelper.ConnectionTestAsync();
            return flag;
        }
    }
}